//
//  HouseTypeInfoView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/25.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HouseTypeInfoView.h"
#import "HouseTypeInfoMessageView.h"
#import "Masonry.h"
#import "LayoutCommentsTableViewCellModel.h"
#import "UITableViewCell+Custom.h"
#import "ViewUtil.h"

@interface HouseTypeInfoView()<UITableViewDataSource,LabelDisclosureIndicatorDelegate>
@property (nonatomic, strong) HouseTypeInfoMessageView *messageView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *tableHeaderView;
@end

@implementation HouseTypeInfoView
#pragma mark - Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadHouseTypeInfoView];
    }
    return self;
}
#pragma mark - Delegate UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id<CustomCellModel> model = _layoutCommentsTableViewCellModels[indexPath.row];
    UITableViewCell *cell = [UITableViewCell tableView:tableView cellWithModel:model];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _layoutCommentsTableViewCellModels.count;
}

#pragma mark - Delegate LabelDisclosureIndicatorDelegate
- (void)disclosureIndicatorChangeEvent:(DisclosureIndicatorViewDir) dir{
    DDLogInfo(@"户型评论查看更多点击事件");
    if (_houseTypeInfoDelegate != nil && [_houseTypeInfoDelegate respondsToSelector:@selector(houseTypeInfoMoreCommentClickAction)]) {
        [_houseTypeInfoDelegate houseTypeInfoMoreCommentClickAction];
    }
}

#pragma mark - Event Response
- (void)musicHtmlButtonAction{
    DDLogInfo(@"户型评论查看更多点击事件1");
    if(_houseTypeInfoDelegate != nil && [_houseTypeInfoDelegate respondsToSelector:@selector(houseTypeInfoMediaHtmlClickActionWithUrlId:)]){
        if (_houseTypeInfoMessageModel.mediaMagazineId != 0) {
           [_houseTypeInfoDelegate houseTypeInfoMediaHtmlClickActionWithUrlId:_houseTypeInfoMessageModel.mediaMagazineId];
        }
    }
}

- (void)htmlButtonAction{
    DDLogInfo(@"户型评论查看更多点击事件2");
    if(_houseTypeInfoDelegate != nil && [_houseTypeInfoDelegate respondsToSelector:@selector(houseTypeInfoHtmlClickActionWithUrlId:)]){
        if (_houseTypeInfoMessageModel.magazineId != 0) {
           [_houseTypeInfoDelegate houseTypeInfoHtmlClickActionWithUrlId:_houseTypeInfoMessageModel.magazineId];
        }
    }
}

- (void)calculateButtonAction{
    DDLogInfo(@"户型评论查看更多点击事件3");
    if(_houseTypeInfoDelegate != nil && [_houseTypeInfoDelegate respondsToSelector:@selector(houseTypeInfoCalculateClickActionWithHouseTotalPrice:)]){
        [_houseTypeInfoDelegate houseTypeInfoCalculateClickActionWithHouseTotalPrice:_houseTypeInfoMessageModel.totalPrice];
    }
}

#pragma mark - Private
- (void)loadHouseTypeInfoView{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    _topView = [UIView new];
    _topView.backgroundColor = BackgroundColor240;
    
    _tableHeaderView = [UIView new];
    _tableHeaderView.backgroundColor = BackgroundColor240;
    [_tableHeaderView addSubview:self.messageView];
    [_tableHeaderView addSubview:_topView];
    self.houseTypeInfoTableView.tableHeaderView = _tableHeaderView;

    LabelDisclosureIndicatorView *indicatorView = [ViewUtil loadSalersTitleWithBackView:_topView withTitle:@"户型评论" andTitleColor:[UIColor colorWithRed:0.2824 green:0.2824 blue:0.2824 alpha:1.0]];
    indicatorView.delegate = self;
    
    [_tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.equalTo(self.houseTypeInfoTableView);
    }];
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableHeaderView);
        make.left.width.equalTo(_tableHeaderView);
    }];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.messageView.mas_bottom);
        make.left.width.equalTo(self.messageView);
        make.height.mas_equalTo(ScreenHeight*0.07);
        make.bottom.equalTo(_tableHeaderView);
    }];
    [self.houseTypeInfoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.equalTo(self);
        make.height.equalTo(self);
    }];
}

#pragma mark - Getter
- (HouseTypeInfoMessageView *)messageView{
    if (_messageView == nil) {
        _messageView = [HouseTypeInfoMessageView new];
        _messageView.backgroundColor = BackgroundColor240;
        [_messageView.musicHtmlButton addTarget:self action:@selector(musicHtmlButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [_messageView.htmlButton addTarget:self action:@selector(htmlButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [_messageView.calculateButton addTarget:self action:@selector(calculateButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _messageView;
}

- (UITableView *)houseTypeInfoTableView{
 
    if (_houseTypeInfoTableView == nil) {
        _houseTypeInfoTableView = [UITableView new];
        _houseTypeInfoTableView.dataSource = self;
        _houseTypeInfoTableView.rowHeight = UITableViewAutomaticDimension;
        _houseTypeInfoTableView.estimatedRowHeight = 60;
        _houseTypeInfoTableView.backgroundColor = BackgroundColor240;
        [self addSubview:_houseTypeInfoTableView];
    }
    return _houseTypeInfoTableView;
}

#pragma mark - Setter
- (void)setHouseTypeInfoMessageModel:(HouseTypeInfoMessageModel *)houseTypeInfoMessageModel{
    _houseTypeInfoMessageModel = houseTypeInfoMessageModel;
    self.messageView.model = houseTypeInfoMessageModel;
}

- (void)setLayoutCommentsTableViewCellModels:(NSArray *)layoutCommentsTableViewCellModels{
    _layoutCommentsTableViewCellModels = layoutCommentsTableViewCellModels;
    UILabel *titleLabel = (UILabel *)[_topView viewWithTag:IndictorLabelTag];
    titleLabel.text = [NSString stringWithFormat:@"户型评论(%ld)",layoutCommentsTableViewCellModels.count];
    [self.houseTypeInfoTableView reloadData];
}

- (void)layoutSubviews{
    [self layoutIfNeeded];
    self.houseTypeInfoTableView.tableHeaderView = _tableHeaderView;
}

@end
